<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

if($_POST && $_FILES && $_POST['upload_path']){
	$file_type=$_POST['file_type']?$_POST['file_type']:'image';
    $type_arr=explode('/',$_FILES['file']['type']);
    $upload_type=$type_arr[1];
    $upload_path=$_POST['upload_path'];
    $oldName = $_FILES['file']['name'];
    //$newName = sha1($oldName).rand(100,999).'.'.$upload_type;
    $newName = sha1($oldName).'.'.$upload_type;
    //$newName = time() . strrchr($oldName, '.');
    $now_date=date('Ymd',time());
    if(!file_exists('.'.$upload_path.'/'.$now_date)){
        mkdir('.'.$upload_path.'/'.$now_date);
    }
    $path = '.'.$upload_path.'/'.$now_date.'/' . $newName;

    $newpath = '';
    if (is_uploaded_file($_FILES['file']['tmp_name'])) {

        if (move_uploaded_file($_FILES['file']['tmp_name'], $path)) {

            $newpath = $upload_path .'/'.$now_date.'/'. $newName;

        } else {
            $newpath = '';
        }
		 if($file_type=='image'){
                    $video_time='';
		}else{
			$file_name=mb_convert_encoding(dirname($_SERVER['SCRIPT_FILENAME']).$newpath,'GB2312',mb_detect_encoding(dirname($_SERVER['SCRIPT_FILENAME']).$newpath));
			$php_exec=shell_exec('ffprobe -v quiet -print_format json -show_format -show_streams "'.$file_name.'"');
			$time_long=floor(json_decode($php_exec)->format->duration);
			$time_long= (new \DateTime('@0')) ->diff(new \DateTime("@$time_long")) ->format('%H:%I:%S');
			//$time_long='';
			$video_time=$time_long;
		}
        if($newpath){
            echo $data = '{"code":"0","msg":"OK","src":"'.$newpath.'","video_time":"'.$video_time.'"}';
        }else{
            echo $data = '{"code":"1003","msg":"error1"}';
        }

    }else{
        echo $data = '{"code":"1003","msg":"error1"}';
    }
}else{
    echo $data = '{"code":"1003","msg":"error1"}';
}


?>